#include "thumbnailwidget.h"
#include <ukui-log4qt.h>
#include <gsettings.hpp>
#include "include/theme.h"

int ThumbnailWidget::scanPictureCount = 0;

ThumbnailWidget::ThumbnailWidget(QWidget *parent) : QListView(parent)
{
    setupGui();
    initConnect();
    initSettings();
    themeChange();

}

//void ThumbnailWidget::paintEvent(QPaintEvent *event)
//{
//    Q_UNUSED(event);
//    QPainter p(this);
//    p.setRenderHint(QPainter::Antialiasing);
//    QPainterPath rectPath;
//    rectPath.addRoundedRect(this->rect(), 0, 0);
//    QStyleOption opt;
//    opt.init(this);

//    QColor mainColor;
//    mainColor = opt.palette.color(QPalette::Base);

//    p.fillPath(rectPath,QBrush(mainColor));
//}
void ThumbnailWidget::setupGui()
{
    this->setFixedWidth(ThumbnailWidgetMinimumWidth);

    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    this->verticalScrollBar()->setProperty("drawScrollBarGroove",false);


    setDragEnabled(false);
    setSpacing(8);
    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setIconSize(ThumbnailIconSize);
    setViewMode(QListView::IconMode);
    setMovement(QListWidget::Static);

    setAcceptDrops(false);
    setFocusPolicy(Qt::NoFocus);
    setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);

}

void ThumbnailWidget::initConnect()
{

    connect(g_user_signal, &GlobalUserSignal::scanThreadFinishedImageLoadSignal, this, &ThumbnailWidget::showNormalImageAfterScan);
    connect(this, &ThumbnailWidget::clicked, this, &ThumbnailWidget::clickedItemSlot);
    connect(kdk::kabase::Gsettings::getPoint(), &kdk::kabase::Gsettings::systemThemeChange, this, &ThumbnailWidget::themeChange);

}
void ThumbnailWidget::themeChange()
{
    QPalette pal(palette());
    if (isDarkTheme()) {
        this->setStyleSheet("QListView{outline:none;background:rgba(38,38, 38, 1);border-radius: 0px;}"
                            "QListView::item{margin:0 1px 0 1px;background:rgba(38, 38, 38, 0.8);border-radius: 6px; }"
                            "QListView::item:selected{border:1px solid #3790FA;background:rgba(38, 38, 38, 0.8);}"
                            "QListView::item:hover{background:rgba(55, 144, 250, 0.5);}");
//        pal.setColor(QPalette::Background, QColor(64, 64, 64));
    } else {
        this->setStyleSheet("QListView{outline:none;background:rgba(255,255, 255, 1);border-radius: 0px;}"
                                "QListView::item{margin:0 1px 0 1px;background:rgba(255, 255, 255, 0.8);border-radius: 6px; }"
                                "QListView::item:selected{border:1px solid #3790FA;background:rgba(255, 255, 255, 0.8);}"
                                "QListView::item:hover{background:rgba(55, 144, 250, 0.5);}");

//        pal.setColor(QPalette::Background, QColor(240, 240, 240));
    }
    setAutoFillBackground(true);
    setPalette(pal);
}
bool ThumbnailWidget::isDarkTheme()
{
    QString systemTheme = kdk::kabase::Gsettings::getSystemTheme().toString();

    if (systemTheme == STYLE_NAME_KEY_DARK || systemTheme == STYLE_NAME_KEY_BLACK) {
        return true;
    } else {
        return false;
    }
}
void ThumbnailWidget::initSettings()
{
    showThumbnailIcon();
}


void ThumbnailWidget::showThumbnailIcon()
{
    m_thumbnailDelegate = new ThumbnailDelegate();
    this->setItemDelegate(m_thumbnailDelegate);

    m_thumbnailItemModel = new QStandardItemModel();

}

void ThumbnailWidget::showNormalImageAfterScan()
{

    if (g_config_signal->m_kylinScannerImageDebug) {
        g_sane_object->loadFullScanFileName = g_config_signal->m_openFileName;
    }

    QString scanFullName = g_sane_object->loadFullScanFileName;
    QFileInfo scanFullNameFileInfo(scanFullName);

    QString tooltipName = scanFullNameFileInfo.baseName();

    int rowcount = m_thumbnailItemModel->rowCount();
    KyInfo() << "rowcount = " << rowcount
             << "scanFullName = " << scanFullName;

    Mat iconMat = KylinImageCodec::loadThumbnailToMat(scanFullName).mat;
    QPixmap iconPix = KylinImageCodec::converFormat(iconMat);

    QPixmap fitpixmap = iconPix.scaled(QSize(40, 40), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);

    qDebug() << "image mesg:" << fitpixmap;
    ScanStandardItem *item = new ScanStandardItem();
    item->setIcon(QIcon(fitpixmap));
    item->setToolTip(tooltipName);
    item->setPath(scanFullName);
    item->setRowCountLocation(rowcount);
    m_thumbnailItemModel->insertRow(rowcount, item);

    // Default choose index
    QModelIndex index = m_thumbnailItemModel->index(rowcount, 0);
    this->setCurrentIndex(index);

    scanPictureCount = rowcount;

    this->setModel(m_thumbnailItemModel);

}

void ThumbnailWidget::clickedItemSlot(const QModelIndex &index)
{
     ScanStandardItem *item = dynamic_cast<ScanStandardItem *>(m_thumbnailItemModel->itemFromIndex(index));
     if (item) {
         int scanIconNumber = item->getRowCountLocation();
         QString loadPath = item->getPath();

         KyInfo() << "scanIconPath = " << scanIconNumber << loadPath;

         g_user_signal->showImageAfterClickedThumbnail(loadPath);

     }
}


void ScanStandardItem::setPath(const QString &path)
{
    m_path = path;
}

QString ScanStandardItem::getPath()
{
    return m_path;
}

int ScanStandardItem::getRowCountLocation() const
{
    return rowCountLocation;
}

void ScanStandardItem::setRowCountLocation(int value)
{
    rowCountLocation = value;
}
